package com.yun.zf.common.utils;

import java.util.ArrayList;
import java.util.List;

public class TreeBuilder {

    public static List<TreeNodes> buildByRecursive(List<TreeNodes> treeNodes,String rootId) {
        List<TreeNodes> trees = new ArrayList<TreeNodes>();
        for (TreeNodes treeNode : treeNodes) {
            if(rootId.equals (treeNode.getParentId())){
                trees.add(findChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }

    public static TreeNodes findChildren(TreeNodes treeNode,List<TreeNodes> treeNodes) {
        for (TreeNodes it : treeNodes) {
            if(treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<TreeNodes>());
                }
                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }
}
